package com.yzion.baselib.http.interceptor

import com.blankj.utilcode.util.LogUtils
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * 自定义的，重试N次的拦截器
 * 通过：addInterceptor 设置
 */
class RetryInterceptor(  //最大重试次数
    var maxRetry: Int
) : Interceptor {
    private var retryNum = 0 //假如设置为3次重试的话，则最多请求4次（默认1次+3次重试）

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        var response: Response = chain.proceed(request)
        while (response.code != 200 && retryNum < maxRetry) {
            retryNum++
            LogUtils.i("Retry", "num:$retryNum")
            response = chain.proceed(request)
        }
        return response
    }
}